﻿@using MvcContrib.UI.Grid;
@using XRM.Security.DataAccess.Enums
@using XRM.Security.WebUI.Consts
@using XRM.Security.WebUI.Security
@model XRM.Security.WebUI.Models.UserRegister.UserRegisterCertEditModel
@{
    Layout = null;
}
@functions {

    readonly string[] userRoles = UserPrincipal.Current.Roles;
    private bool IsUserInRole(IEnumerable<string> roles)
    {
        return roles.Intersect(userRoles).Any();
    }
}
<div class="row">
    @if ((Model.UserStatus == UserStatus.Work) && (IsUserInRole(new[] { UserRoles.Administrator.ToString(), UserRoles.UsersReestrSertificateEditor.ToString() })))
    {
        
        <form id="addCertForm" action="@Url.Action("Add", "CertRegister")" method="POST">
            <div class="col-sm-offset-3 col-sm-6">
                @Html.Partial("CertUploader", new ViewDataDictionary { { ViewDataConsts.Callback, "onCertUpload" } })
            </div>
            <input type="hidden" name="userId" value="@Model.UserRegisterId"/>
        </form>
        
        <script type="text/javascript">
            function onCertUpload() {
                $("#addCertForm").submit();
            }
        </script>
    }
</div>
<div class="filtered-table">
    @{
        if (Model.CertList.Any())
        {
            if ((Model.UserStatus == UserStatus.Work) && (IsUserInRole(new[] { UserRoles.Administrator.ToString(), UserRoles.UsersReestrSertificateEditor.ToString() })))
            {
            <hr />
            }
            <div class="checkbox">
                <label for="ShowAllCert">
                    <input type="checkbox" id="ShowAllCert" />
                    Отобразить все сертификаты
                </label>
            </div>
            
            Html.Grid(Model.CertList).Columns(column =>
            {
                if (IsUserInRole(new[] { UserRoles.Administrator.ToString(), UserRoles.UsersReestrSertificateEditor.ToString() }))
                {
                    column.For(item => Html.ActionLink(item.SerialNumber, "Edit", "CertRegister", new { id = item.Id }, null)).Named("Серийный номер сертификата");
                }
                else
                {
                    column.For(item => item.SerialNumber).Named("Серийный номер сертификата");
                }
                column.For(item => item.SerialNumberEtoken).Named("Серийный номер etoken");
                column.For(item => item.Period).Named("Срок действия");
                column.For(item => item.IdentityCenterName).Named("УЦ");
                column.Custom(@<text>
                                       @foreach (var infoSystem in item.InfoSystems)
                                       {
                                           <div>@infoSystem</div>
                                       }
                                   </text>).Named("Информационные системы");
                column.For(item => item.PCSerialNumber).Named("Серийный номер ПК");
                column.Custom(@<text>
                                   <i class="fa fa-action @item.StatusIcon" title="@item.Status.Description()" rel="tooltip"></i>
                                   <span class="hide">@item.Status.Description()</span>
                               </text>).Named("Статус").Attributes(style => "min-width: 60px; text-align: center;");
                column.Custom(@<text>
                                    @if (item.RequestId.HasValue)
                                    {
                                        @Html.ActionLink(item.RequestNumber.ToString(), "Edit", "Request", new { id = item.RequestId }, null)
                                    }
                                    else
                                    {
                                        <text>Ручной ввод</text>
                                    }
                                  </text>).Named("Заявка");

                column.Custom
                        (@<text>
                                  <a href="@Url.Action("Download", "CertRegister", new { id = item.Id })" title="Скачать"> <i class="fa fa-download fa-action"></i></a>
                              </text>).Named("").Attributes(nowrap => "nowrap");


            }).Attributes(@class => "table table-striped table-bordered", id => "table_usercert_id")
            .RowAttributes( item => new MvcContrib.Hash( @class => item.Item.Status == CertStatus.NotValid ? "inactive hide" : "" ) )
            .Render();
        }
        else
        {
            <p>Сертификаты отсутствуют.</p>
        }
    }
</div>
   @Scripts.Render("~/bundles/gridlibrary")
   <script type="text/javascript" src="@(Url.Content("~/Scripts/FileUploader.js"))"></script>

<script type="text/javascript">
    $("#ShowAllCert").change(function () {
        if ($(this).is(":checked")) {
            $("#table_usercert_id .inactive").removeClass("hide");
        }
        else {
            $("#table_usercert_id .inactive").addClass("hide");
        }
    });
    
    $(document).ready(function () {
        @if (Model.CertList.Any())
           {
                <text>
                $('#table_usercert_id').dataTable({
                    paging: false,
                    bFilter: false,
                    bSort: false,
                    info: false
                });
                </text>
           }
        @if(Model.CertList.All(s => s.Status == CertStatus.NotValid))
        {
            @:$("#ShowAllCert").attr("checked", "checked");
        }
        $("#ShowAllCert").change();
    });
</script>
